package com.briup.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class StudentManagement {

     private Students[] students=new Students[10]; 
     private Scanner scanner=new Scanner(System.in);
     private int studentnum=0;
    
     public void st(){
        students[0]=new Students("李霄", 216);
        students[1]=new Students("剑帝", 217);
        students[2]=new Students("梁蛋", 220);
        studentnum+=3;
     }

     //判断id是否相同
     public boolean Id11(int id){
        for(int i=0;i<studentnum;i++){
            if(students[i]!=null&&id==students[i].getId()){
                return true;
            }
        }
        return false;
     }


     //add
     public void AddSt(){
        System.out.println("请输入添加姓名：");
        String name=scanner.nextLine();
        System.out.println("请输入添加学号:");
        int id=scanner.nextInt();
        scanner.nextLine();
        if(Id11(id)){System.out.println("已有此人，无需添加！");}
        else {
            studentnum++;
            if(studentnum>students.length){Arrays.copyOf(students,studentnum+1);}
            students[studentnum-1]=new Students(name,id);
            System.out.println("添加成功");
        }
     }
     //delete
     public void Delete(){
        System.out.println("请输入学号:");
        int id=scanner.nextInt();
        if(Id11(id)){System.out.println("删除成功!!!");
        for(int i=0;i<studentnum;i++){
             if(students[i]!=null&&id==students[i].getId()){
                studentnum--;
                students[i]=students[studentnum];
                students[studentnum]=null;
             }
        }
    }
        else {System.out.println("查无此人。");}
     }
    //Update修改
    public void Update(){
        System.out.println("请输入学号:");
        int id=scanner.nextInt();
        if(Id11(id)){System.out.println("您可以开始修改：");
        for(int i=0;i<studentnum;i++){
        if(students[i]!=null&&id==students[i].getId()){
            System.out.println("请输入姓名:");
            String name=scanner.nextLine();
            System.out.println("请输入学号：");
            int id2=scanner.nextInt();
            students[i]=new Students(name,id);
            System.out.println("修改成功！！！");
        } 
        else System.out.println("查无此人！！！");
    }
    }
    }
    //Select
    public void Select(){
        System.out.println("请输入学号：");
        int id=scanner.nextInt();
        if(Id11(id)){System.out.println("OK，已经为您找到啦！");
        for(int i=0;i<studentnum;i++){
            if(students[i]!=null&&id==students[i].getId()){
                System.out.println(students[i]);
            }
        }
        }
    }

    //Quit

    //打印
    public void show1(){
        String a="姓名";
        String b="学号";
        System.out.printf("%5s %5s",a,b);
        System.out.println();
        for(int i=0;i<studentnum;i++){
            System.out.printf("%5s  %5d",students[i].getName(),students[i].getId());
            System.out.println();
        }
    }




    //main
    public void show(){
        System.out.println("------------------------------");
        System.out.println("1.add");
        System.out.println("2.delete");
        System.out.println("3.uodate");
        System.out.println("4.select");
        System.out.println("5.Quit");
        System.out.println("6.show");
        System.out.println("------------------------------");
        Scanner in=new Scanner(System.in);
        boolean fo=true;
        while (fo) {        
        System.out.println("请输入您的选择。");
        int a=in.nextInt();

        switch (a) {
            case 1:
                AddSt();
                break;
            case 2:
                Delete();
                break;
            case 3:
                Update();
                break;
            case 4:
                Select();
                break;
            case 5:
                fo=false;
                break;
            case 6:
                show1();
                break;
            default:
                break;
        }

    }System.out.println("您已退出。");
}
}








